package com.fr.lintcode;

import java.util.HashSet;
import java.util.Set;

public class Q488 {

    /**
     * @param n:
     *            An integer
     * @return: true if this is a happy number or false
     */
    public boolean isHappy(int n) {
        // write your code here
        Set<Integer> set = new HashSet<>();
        while (set.add(n)) {
            n = cal(n);
            if (n == 1) {
                return true;
            }
        }
        return false;

    }

    private int cal(int n) {
        int sum = 0;
        while (n > 0) {
            int t = n % 10;
            sum = sum + t * t;
            n = n / 10;
        }
        return sum;
    }

}
